package org.deltaset.meta.faces;

import java.io.Serializable;
import java.util.ArrayDeque;
import java.util.Deque;

/**
 *
 * @author dmakariev
 */
class HistoryStack implements Serializable {

    private final Deque<AttributeContext> stack;

    public HistoryStack() {
        stack = new ArrayDeque<AttributeContext>();
    }

    boolean isEmpty() {
        return stack.isEmpty();
    }

    boolean hasPrevious() {
        final boolean isEmpty = stack.size() < 2;
        return isEmpty;
    }

    void push(final AttributeContext attributeContext) {
        final AttributeContext last = stack.peek();
        final boolean isTheSameAsCurrentLast = attributeContext.equals(last);
        final boolean isEmpty = attributeContext.isEmpty();
        if (false == isTheSameAsCurrentLast
                && false == isEmpty) {
            stack.push(attributeContext);
        }
    }

    AttributeContext previous() {
        if (!stack.isEmpty()) {
            stack.pop();
        }
        if (stack.isEmpty()) {
            return stack.peek();
        }
        return null;
    }

    AttributeContext current() {
        return stack.peek();
    }
}
